package com.hzw.saas.service.user.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.ds.enums.ClientTypeEnum;
import com.hzw.saas.api.user.IUserProfileService;
import com.hzw.saas.api.user.bo.DsUserAccountBo;
import com.hzw.saas.api.user.bo.UserProfileBO;
import com.hzw.saas.common.config.exception.DefaultSaasException;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.service.user.mapper.UserProfileMapper;
import com.hzw.saas.service.user.model.UserProfile;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 用户配置表 服务实现类
 * </p>
 *
 * @author sonam
 * @since 2020-12-21
 */
@Service
@AllArgsConstructor
@Slf4j
public class UserProfileServiceImpl extends ServiceImpl<UserProfileMapper, UserProfile> implements IUserProfileService, IService<UserProfile> {

    private final MapperFacade mapperFacade;

    @Override
    public List<UserProfileBO> getUserProfiles(List<String> userId) {
        if (CollectionUtil.isEmpty(userId)){
            return Collections.emptyList();
        }
        List<UserProfile> list = this.lambdaQuery().in(UserProfile::getUserId, userId).list();
        return MapperUtil.nf().mapAsList(list,UserProfileBO.class);
    }

    @Override
    public UserProfileBO add(UserProfileBO userProfileBO) {
        AssertUtil.assertThrow("用户ID为空", StringUtils.isBlank(userProfileBO.getUserId()));
        UserProfile userProfile = mapperFacade.map(userProfileBO, UserProfile.class);
        this.save(userProfile);
        return userProfileBO;
    }

    @Override
    public UserProfileBO get(String userId) {
        if (StringUtils.isBlank(userId)) {
            return null;
        }
        UserProfile userProfile = this.baseMapper.selectById(userId);
        return mapperFacade.map(userProfile, UserProfileBO.class);
    }

    @Override
    public boolean update(UserProfileBO userProfileBO) {
        if (StringUtils.isBlank(userProfileBO.getUserId())) {
            throw DefaultSaasException.build("用户ID为空").status(HttpStatus.BAD_REQUEST);
        }
        UserProfile userProfile = mapperFacade.map(userProfileBO, UserProfile.class);
        return this.updateById(userProfile);
    }

    @Override
    public DsUserAccountBo getDsUserAccount(String userId, ClientTypeEnum clientType) {
        AssertUtil.assertThrow("用户ID为空", StringUtils.isBlank(userId));
        return this.baseMapper.getDsUserAccount(userId, clientType);
    }
}
